package com.zoo.diannaopeijian.service.impl;

import com.github.pagehelper.PageHelper;
import com.zoo.diannaopeijian.domain.Warehouse;
import com.zoo.diannaopeijian.domain.WarehouseExample;
import com.zoo.diannaopeijian.mapper.WarehouseMapper;
import com.zoo.diannaopeijian.service.WarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zx583 on 2018-01-10.
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public List<Warehouse> getWarehouse(Warehouse warehouse) {
        WarehouseExample example = new WarehouseExample();

        if (warehouse != null) {
            WarehouseExample.Criteria criteria = example.createCriteria();

            if (warehouse.getWarehouseid() != null) {
                criteria.andWarehouseidEqualTo(warehouse.getWarehouseid());
            }
            if (!StringUtils.isEmpty(warehouse.getWarehouselocation())) {
                criteria.andWarehouselocationEqualTo(warehouse.getWarehouselocation());
            }
            if (!StringUtils.isEmpty(warehouse.getWarehousename())) {
                criteria.andWarehousenameEqualTo(warehouse.getWarehousename());
            }

        }

        return warehouseMapper.selectByExample(example);
    }

    @Override
    public List<Warehouse> getWarehouseWithPage(Integer page, Integer rows, Warehouse warehouse) {
        PageHelper.startPage(page, rows);

        return getWarehouse(warehouse);
    }

    @Override
    public List<Warehouse> getFuzzyWarehouse(Warehouse warehouse) {
        return warehouseMapper.selectFuzzyWarehouse(warehouse);
    }

    @Override
    public List<Warehouse> getFuzzyWarehouseWithPage(Integer page, Integer rows, Warehouse warehouse) {
        PageHelper.startPage(page, rows);

        return getFuzzyWarehouse(warehouse);
    }

    @Override
    public boolean addWarehouse(Warehouse warehouse) {
        return warehouseMapper.insertSelective(warehouse) == 1;
    }

    @Override
    public boolean deleteWarehouse(Integer warehouseid) {
        return warehouseMapper.deleteByPrimaryKey(warehouseid) == 1;
    }

    @Override
    public boolean updateWarehouse(Warehouse warehouse) {
        return warehouseMapper.updateByPrimaryKeySelective(warehouse) == 1;
    }
}
